<?php

class UserFollowController extends BaseController
{
    protected $tableName = "user_follow";
    protected $pk = "follow_id";


    public function post($obj)
    {
        $follower = $obj["follower"];
        $target = $obj["target"];
        if ($follower === $target) {
            return ReturnHelper::failed([], "用户`$follower`不能关注自己");
        }
        $sql = "SELECT * FROM user_follow WHERE follower=$follower AND target=$target";
        $result = $this->conn->query($sql);
        $data = $this->getSingleDataFromResult($result);
        if (empty($data)) {
            $sql = "INSERT INTO user_follow VALUES(0, $follower, $target)";
            $result = $this->conn->query($sql);
            if ($this->judge($result)) {
                $newRecord = $this->getNewRecord($obj);
                return ReturnHelper::success($newRecord, "用户`$follower`关注用户`$target`成功");
            }
            else {
                return ReturnHelper::success([], "用户`$follower`关注用户`$target`失败");
            }
        }
        else {
            return ReturnHelper::failed([], "用户`$follower`已经关注了`$target`，不能重复关注");
        }
    }
}